#include "Player.h"
#include <sstream>

void CPlayer::Init(HDC graphics)
{
	y = 0;
	x = 0;
	spawnX = 0;
	spawnY = 0;
	checkpoint = -1;
	height = 32;
	width = 32;
	fheight = 77;
	fwidth = 72;
	health = 100;
	waldoTime = 0;

	angle = 180.0;
	jetpackAngle = 0;
	char picfile[] = "res/jbrocko32.bmp";
	hImage = LoadImage(NULL, picfile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	hFlame = LoadImage(NULL, "res/flame0.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

	bufDC = CreateCompatibleDC(graphics);
	bufBMP = CreateCompatibleBitmap(graphics, rect.right, rect.bottom);
	SelectObject(bufDC, bufBMP);
	::FillRect(bufDC, &rect, ::CreateSolidBrush(0x00FFFFFF));

	
	char **names;
	names=new char* [6];
	for(int i=0;i<100;i++)
	*(names+i)=new char[255];

	names[0] = "res/anim1.bmp";
	names[1] = "res/anim2.bmp";
	names[2] = "res/anim3.bmp";
	names[3] = "res/anim4.bmp";
	names[4] = "res/anim5.bmp";
	names[5] = "res/anim6.bmp";

	//a = new Animation(names, 6);
	
	xSpeed= 0;
	ySpeed=  0;

	brocko = new HANDLE[3];
	brocko[0] = LoadImage(NULL, "res/jbrocko32.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	brocko[1] = LoadImage(NULL, "res/jbrockoflipped32.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	brocko[2] = LoadImage(NULL, "res/waldo32.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	brocko[3] = LoadImage(NULL, "res/waldoflipped32.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

	jetpack = new HANDLE[12];
	for(int i = 0; i < 12; i++){
		stringstream ss;
		ss << "res/flame" << (i*30) << ".bmp"; 
		jetpack[i] = LoadImage(NULL, ss.str().c_str() , IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	}

	throttle = 0;
	this->deaths = 0;
	this->score = 0;
	resumeTime = 0;

}
CPlayer::CPlayer(void)
{
	
}

CPlayer::~CPlayer(void)
{
}


void CPlayer::IncreaseAngle(double value){

	if((jetpackAngle >= 180 && value > 0) || (jetpackAngle <= 180 && value < 0)){
		value -= (value /4);
	}

	jetpackAngle += value;
	while(jetpackAngle >= 360.0){
		jetpackAngle -= 360.0;
	}
	while (jetpackAngle < 0.0){
		jetpackAngle += 360.0;
	}
}

void CPlayer::IncreaseThrottle(double value){
	throttle += value;
	while(throttle > 10.0){
		throttle = 10.0;
	}
	while (throttle < 0.0){
		throttle = 0.0;
	}
}

void CPlayer::Update()
{
	using namespace std;

		ySpeed -= ySpeed/60;
		xSpeed -= xSpeed/80;

		if(xSpeed > 5){
			xSpeed = 5;
		}
		if(xSpeed < -5){
			xSpeed = -5;
		}

		if(xSpeed < 0){
			xSpeed += 0.011;
		}
		else if(xSpeed > 0){
			xSpeed -= 0.011;
		}
	
		if(!tiles->canGoThere(x + 2, y + 5, 28,28) && ySpeed < 1 && ySpeed > -1){
			xSpeed -= xSpeed/25; 
		}


	double lengteX =   sin( jetpackAngle * (3.14159265 / 180.0)) * (throttle / 50);
	double lengteY =   cos( jetpackAngle * (3.14159265 / 180.0) ) * (throttle / 50);

	lengteX *= -1;

	xSpeed += lengteX;
	ySpeed += lengteY;

	double xTest = x + xSpeed;

	ySpeed += 0.07;



	bool midAir = true;
	if(!tiles->canGoThere(x + 2, y + 5, 28,28) && ySpeed < 1 && ySpeed > -1){
		 midAir = false;;
		}

	if(input->upPressed){
		IncreaseThrottle(1.0);
	}
	 if(input->downPressed){
		IncreaseThrottle(-6.0);
	}
	if(input->rightPressed){

		IncreaseAngle(6);
	
	}

	if(input->leftPressed){
	//	IncreaseAngle(-1);
		//xTest -= 3;

		IncreaseAngle(-6);

	}

		//stringstream ss;//create a stringstream

		//ss << "res/flame";

		if(jetpackAngle > 345 || jetpackAngle <= 15){
			hFlame = jetpack[0];
			isFlipped = false;
		}
		else if(jetpackAngle <= 45){
			hFlame = jetpack[1];
			isFlipped = true;
		}
		else if(jetpackAngle <= 75){
			hFlame = jetpack[2];
			isFlipped = true;
		}
		else if(jetpackAngle <= 105){
			hFlame = jetpack[3];
			isFlipped = true;
		}
		else if(jetpackAngle <= 135){
			hFlame = jetpack[4];
			isFlipped = true;
		}
		else if(jetpackAngle <= 165){
			hFlame = jetpack[5];
			isFlipped = true;
		}
		else if(jetpackAngle <= 195){
			hFlame = jetpack[6];
			isFlipped = false;
		}
		else if(jetpackAngle <= 225){
			hFlame = jetpack[7];
			isFlipped = false;
		}
		else if(jetpackAngle <= 255){
			hFlame = jetpack[8];
			isFlipped = false;
		}
		else if(jetpackAngle <= 285){
			hFlame = jetpack[9];
			isFlipped = false;
		}
		else if(jetpackAngle <= 315){
			hFlame = jetpack[10];
			isFlipped = false;
		}
		else{
			hFlame = jetpack[11];
			isFlipped = false;
		}

		if(isFlipped){
			hImage = brocko[1];
		}
		else{
			hImage = brocko[0];
		}

		//ss << ".bmp";//add number to the stream
		//hFlame = LoadImage(NULL, ss.str().c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);



	float yTest = y + ySpeed;


	if(tiles->canGoThere(x + 2, yTest + 2, 28,28)){
		y = yTest;
		
	}
	else{
		ySpeed =  0;
	}

		if(tiles->canGoThere(xTest + 2, y + 2, 28,28)){
		x = xTest;
		
	}

		if(throttle > 0){
			IncreaseThrottle(-0.4);
		}

		RenderHelper::SetViewPoint(x - 400 ,y - 300);

		if(waldoTime > 0){
			if(isFlipped){
				hImage = brocko[3];
			}
			else{
				hImage = brocko[2];
			}
			waldoTime--;
		}

		if(resumeTime > 0){
			resumeTime--;
		}
		else{
			CSoundPlayer::canPlay = true; 
		}
}



void CPlayer::Draw(HDC bufDC)
{
	//Update();
	//if(input->upPressed){
		RenderHelper::RenderBitmap(bufDC, hFlame,x-22, y-21, fwidth, fheight);	
	//}
	RenderHelper::RenderBitmap(bufDC, hImage, x, y, width, height);

	///a->draw(bufDC, 30,30, 32,32);

}

void CPlayer::setInput(InputHandler* handler){
	input = handler;
}

void CPlayer::die(){
	health = health - 100;
	CSoundPlayer::PlaySound1("music\\ouch.wav");
	CSoundPlayer::canPlay = false;
	resumeTime = 60;
	if(health <= 0)
	{

		x = spawnX;
		y = spawnY;
		ySpeed= 0;
		xSpeed= 0;
		this->deaths++;
		health = health + 100;
	}
}

string CPlayer::getDeaths()
{
	string deaths;
	stringstream out;
	out << this->deaths;
	deaths = out.str();
	return deaths;
}

string CPlayer::getHealth()
{
	string strHealth;
	stringstream out;
	out << this->health;
	strHealth = out.str();
	return strHealth;
}

string CPlayer::getScore()
{
	string points;
	stringstream out;
	out << this->score;
	points = out.str();
	return points;
}

void CPlayer::setXSpeed(double x)
{
	xSpeed = x;
}

void CPlayer::setYSpeed(double y)
{
	ySpeed = y;
}

void CPlayer::setDeaths(int deaths)
{
	this->deaths = deaths;
}

void CPlayer::setScore(int score)
{
	this->score = score;
}

void CPlayer::addScore(int point)
{
	this->score += point;
}